//
//  TMPlug.m
//  Sultry
//
//  Created by zhouliangfei on 2016/11/24.
//  Copyright © 2016年 zhouliangfei. All rights reserved.
//
#import "TMPlug.h"
#import "TMZiper.h"
#import "TMLoader.h"

//
#pragma mark-
#pragma mark TMPlugManager
@interface TMPlugManager : NSObject <TMLoaderDelegate>
@property(nonatomic, copy) void (^complete)(id target);
@property(nonatomic, strong) NSMutableArray *plugs;
@property(nonatomic, strong) TMLoader *loader;
@end

@implementation TMPlugManager
+(TMPlugManager*)share{
    static TMPlugManager *instance = nil;
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        instance = [[TMPlugManager alloc] init];
        NSLog(@"%@",[instance directory]);
    });
    return instance;
}
-(instancetype)init{
    self = [super init];
    if (self) {
        [self setLoader:[[TMLoader alloc] initWithDelegate:self cache:2]];
    }
    return self;
}
-(id)regeditPlug:(NSString*)plug{
    NSString *path = [[self directory] stringByAppendingPathComponent:plug];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        NSBundle *bundle = [NSBundle bundleWithPath:path];
        if (bundle && [bundle load]) {
            return bundle;
        }
    }
    return nil;
}
-(void)loadLibrary:(NSArray*)data{
    [self setPlugs:[NSMutableArray arrayWithArray:data]];
    [self loadNext];
}
-(void)loadNext{
    NSString *src = [self.plugs lastObject];
    if (src) {
        [self.plugs removeLastObject];
        //
        NSString *plug = [[src lastPathComponent] stringByDeletingPathExtension];
        NSString *path = [[self directory] stringByAppendingPathComponent:plug];
        if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
            if (self.complete) {
                self.complete(plug);
            }
            [self loadNext];
        }else{
            NSURL *url = [NSURL URLWithString:src];
            if(url){
                NSURLRequest *request = [NSURLRequest requestWithURL:url];
                [self.loader load:request file:nil];
            }else{
                [self loadNext];
            }
        }
    }
}
-(void)completeLoader:(TMLoader *)loader error:(NSError*)error{
    if (error == nil) {
        TMZiper *zip = [[TMZiper alloc] initWithFile:loader.filePath];
        [zip release:[self directory]];
        //
        if (self.complete) {
            NSString *plug = [loader.file stringByDeletingPathExtension];
            self.complete(plug);
        }
    }
    [self loadNext];
}
-(NSString*)directory{
    NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *directory = [documents stringByAppendingPathComponent:@"plugs"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:directory] == NO) {
        [[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return directory;
}
@end


//
#pragma mark-
#pragma mark TMPlug
@implementation TMPlug
+(void)loadLibrary:(NSArray*)data complete:(void (^)(NSString *plug))complete{
    [[TMPlugManager share] setComplete:complete];
    [[TMPlugManager share] loadLibrary:data];
}
+(id)regeditPlug:(NSString*)plug{
    return [[TMPlugManager share] regeditPlug:plug];
}
@end


//
#pragma mark-
#pragma mark initMain
id initMain(NSString *name ,NSBundle *bundle){
    Class main = [bundle classNamed:name];
    if (main) {
        return [main new];
    }
    return nil;
}
